package com.kazu.index.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kazu.index.entity.dao.IndexImgs;
import com.kazu.index.entity.to.IndexImgsTo;
import com.kazu.index.entity.vo.IndexImgsVo;
import com.kazu.index.service.IndexImgsService;
import com.kazu.utils.entity.R;
import com.kazu.utils.exception.handler.CastomException;
import com.kazu.utils.validator.InsertGroup;
import com.kazu.utils.validator.UpdateGroup;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 首页照片集 前端控制器
 * </p>
 *
 * @author kazu
 * @since 2023/02/19 09:41
 */
@RestController
@RequestMapping("/index/indexImgs")
public class IndexImgsController {
	
	/**
	 * 服务对象
	 */
	@Autowired
	private IndexImgsService indexImgsService;
	
	/**
	 * 查询所有数据非分页
	 *
	 * @return 所有数据
	 */
	@GetMapping("selectAll")
	public R selectAll() {
		List<IndexImgs> indexImgs = indexImgsService.getAll();
		List<IndexImgsTo> data = indexImgs.stream().map(item -> {
			IndexImgsTo indexImgsTo = new IndexImgsTo();
			BeanUtils.copyProperties(item, indexImgsTo);
			return indexImgsTo;
		}).collect(Collectors.toList());
		return R.ok().put("data", data);
	}
	
	/**
	 * 分页查询所有数据
	 *
	 * @param current 当前页数
	 * @param limit   每页记录数
	 * @return 所有数据
	 */
	@GetMapping("selectPage/{current}/{limit}")
	public R selectPage(@PathVariable Long current, @PathVariable Long limit) {
		Page<IndexImgs> page = new Page<>(current, limit);
		indexImgsService.page(page, null);
		long total = page.getTotal();
		List<IndexImgs> data = page.getRecords();
		return R.ok().put("total", total).put("data", data);
	}
	
	/**
	 * 通过主键查询单条数据
	 *
	 * @param id 主键
	 * @return 单条数据
	 */
	@GetMapping("selectOne/{id}")
	public R selectOne(@PathVariable Long id) {
		IndexImgs data = indexImgsService.getById(id);
		return R.ok().put("data", data);
	}
	
	/**
	 * 新增数据
	 *
	 * @param indexImgsVo 传入对象
	 * @return 新增结果
	 */
	@PostMapping("insert")
	public R insert(@RequestBody @Validated({InsertGroup.class}) IndexImgsVo indexImgsVo) {
		IndexImgs indexImgs = new IndexImgs();
		BeanUtils.copyProperties(indexImgsVo, indexImgs);
		boolean result = indexImgsService.save(indexImgs);
		if (result) {
			return R.ok();
		} else {
			throw new CastomException(500, "无法执行新增操作");
		}
	}
	
	/**
	 * 修改数据
	 *
	 * @param indexImgsVo 传入对象
	 * @return 修改结果
	 */
	@PutMapping("update")
	public R update(@RequestBody @Validated({UpdateGroup.class}) IndexImgsVo indexImgsVo) {
		IndexImgs indexImgs = indexImgsService.getOne(new QueryWrapper<IndexImgs>()
				.eq("id", indexImgsVo.getId()));
		indexImgs.setImgUrl(indexImgsVo.getImgUrl());
		indexImgs.setIsDeleted(indexImgsVo.getIsDeleted());
		boolean result = indexImgsService.updateById(indexImgs);
		if (result) {
			return R.ok();
		} else {
			throw new CastomException(500, "无法执行修改操作");
		}
	}
	
	/**
	 * 删除数据
	 *
	 * @param idList 主键集合
	 * @return 删除结果
	 */
	@DeleteMapping("delete")
	public R delete(@RequestParam("idList") List<Long> idList) {
		boolean result = indexImgsService.removeBatchByIds(idList);
		if (result) {
			return R.ok();
		} else {
			throw new CastomException(500, "无法执行删除操作");
		}
	}
	
}
